#include "led.h"
#include "monake.h"
#include "kangtu.h"

enum {
	ON = 0,
	OFF,
};

LSD_t lsd_t;
static void led_control(uint8_t num, uint8_t state)
{
	switch(num) {
		case 2:
			if(state == OFF) {
				gpio_bits_set(GPIOB, GPIO_PINS_9);
			} else {
				gpio_bits_reset(GPIOB, GPIO_PINS_9);
			}
			break;
		case 3:
			if(state == OFF) {
				gpio_bits_set(GPIOB, GPIO_PINS_8);
			} else {
				gpio_bits_reset(GPIOB, GPIO_PINS_8);
			}
			break;
		case 4:
			if(state == OFF) {
				gpio_bits_set(GPIOB, GPIO_PINS_7);
			} else {
				gpio_bits_reset(GPIOB, GPIO_PINS_7);
			}
			break;
		default:
			break;
	}
	
}

static void relay_control(uint8_t num, uint8_t state)
{
	switch(num) {
		case 1:
			if(state == ON) {
				gpio_bits_set(GPIOB, EY1_PIN);
			} else {
				gpio_bits_reset(GPIOB, EY1_PIN);
			}
			break;
		case 2:
			if(state == ON) {
				gpio_bits_set(GPIOB, EY2_PIN);
			} else {
				gpio_bits_reset(GPIOB, EY2_PIN);
			}
			break;
		case 3:
			if(state == ON) {
				gpio_bits_set(GPIOB, EY3_PIN);
			} else {
				gpio_bits_reset(GPIOB, EY3_PIN);
			}
			break;
		default:
			break;
	}
	
}


void led_relay_control(uint8_t mode)
{
	if(mode == 0) {
		if(monake_ptu->singles.bit.online == 1) {
			led_control(2, ON);
			if(lsd_t.run_state != 0) {
				relay_control(1, ON);
				relay_control(2, OFF);
				relay_control(3, OFF);
			} else {
				if((lsd_t.door_state != 4) && (lsd_t.door_state != 5)) {
					relay_control(1, OFF);
					relay_control(2, ON);
					relay_control(3, OFF);
				} else {
					relay_control(1, OFF);
					relay_control(2, OFF);
					relay_control(3, ON);
				}
			}
		} else {
			led_control(2, OFF);
			relay_control(1, OFF);
			relay_control(2, OFF);
			relay_control(3, OFF);
		}
	} else {
		if(kangtu_ptu->singles.bit.online == 1) {
			led_control(2, ON);
			if((lsd_t.run_state & 0x80) == 0x80) {
				relay_control(1, ON);
				relay_control(2, OFF);
				relay_control(3, OFF);
			} else {
				if((lsd_t.door_state == 0x01) || (lsd_t.door_state == 0x05)) {
					relay_control(1, OFF);
					relay_control(2, ON);
					relay_control(3, OFF);
				} else {
					relay_control(1, OFF);
					relay_control(2, OFF);
					relay_control(3, ON);
				}
			}
		} else {
			led_control(2, OFF);
			relay_control(1, OFF);
			relay_control(2, OFF);
			relay_control(3, OFF);
		}
	}
	
}

